import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export var constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    meta: { title: 'page404' },
    hidden: true
  }

]

if (process.env.VUE_APP_ROLE === 'ctrl') {
  constantRoutes.push({
    path: '/screen',
    component: () => import('@/views/screen/cast'),
    meta: { title: '投屏功能' },
    hidden: true
  })
  constantRoutes.push({
    path: '/aucer',
    component: () => import('@/views/screen/aucer'),
    meta: { title: '大屏幕' },
    hidden: true
  })
}

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
var asyncRoutes_super = []
var asyncRoutes_sub = []
var asyncRoutes_ctrl = []
var asyncRoutes_aucer = []
var asyncRoutes_ctrlbid = []
var asyncRoutes_bidco = []

if (process.env.VUE_APP_ROLE === 'super') {
  asyncRoutes_super = [
    {
      path: '/',
      component: Layout,
      redirect: '/sub',
      meta: { title: '代理管理', icon: 'tree', roles: ['admin'] },
      children: [
        {
          path: '/sub',
          name: 'SubManagement',
          hidden: true,
          component: () => import('@/views/super/sub'),
          meta: { title: '代理管理', breadcrumb: false, activeMenu: '/' }
        },
        {
          path: 'user/:subid(\\d+)',
          name: 'UserManagement',
          hidden: true,
          component: () => import('@/views/user/list'),
          meta: { title: '会员管理' }
        }
      ]
    },
    {
      path: '/bidco',
      component: Layout,
      redirect: '',
      children: [
        {
          path: '',
          name: 'BidCoManagement',
          component: () => import('@/views/user/bidco'),
          meta: { title: '代拍管理', icon: 'bidco', roles: ['admin'] }
        }
      ]
    },
    // 404 page must be placed at the end !!!
    { path: '*', redirect: '/404', hidden: true }
  ]
}

if (process.env.VUE_APP_ROLE === 'sub') {
  asyncRoutes_sub = [
    {
      path: '/',
      component: Layout,
      redirect: '/',
      meta: { title: '专场管理', icon: 'lot', roles: ['admin'] },
      children: [
        {
          path: '',
          name: 'LotManagement',
          hidden: true,
          component: () => import('@/views/lot/subList'),
          meta: { title: '专场管理', breadcrumb: false }
        },
        {
          path: ':id(\\d+)',
          name: 'LotGoods',
          hidden: true,
          component: () => import('@/views/goods/list'),
          meta: { title: '专场拍品' }
        }
      ]
    },
    {
      path: '/',
      component: Layout,
      redirect: '/',
      meta: { title: '全部专场', icon: 'lot', roles: ['auditor'] },
      children: [
        {
          path: '',
          name: 'AllLot',
          hidden: true,
          component: () => import('@/views/lot/commonList'),
          meta: { title: '全部专场', breadcrumb: false }
        },
        {
          path: ':id(\\d+)',
          name: 'LotGoods',
          hidden: true,
          component: () => import('@/views/goods/list'),
          meta: { title: '专场拍品' }
        }
      ]
    },
    {
      path: '/',
      component: Layout,
      redirect: 'user',
      meta: { roles: ['editor', 'cashier'] },
      children: [
        {
          path: 'user',
          name: 'UserManagement',
          component: () => import('@/views/user/list'),
          meta: { title: '会员管理', icon: 'users' }
        }
      ]
    },
    {
      path: '/user',
      component: Layout,
      redirect: '',
      meta: { roles: ['admin'] },
      children: [
        {
          path: '',
          name: 'UserManagement',
          component: () => import('@/views/user/list'),
          meta: { title: '会员管理', icon: 'users' }
        }
      ]
    },
    {
      path: '/bidco',
      component: Layout,
      redirect: '',
      meta: { roles: ['admin', 'cashier'] },
      children: [
        {
          path: '',
          name: 'BidCoManagement',
          component: () => import('@/views/user/bidco'),
          meta: { title: '代拍管理', icon: 'bidco' }
        }
      ]
    },
    {
      path: '/',
      component: Layout,
      redirect: 'goods/out',
      meta: { roles: ['outer'] },
      children: [
        {
          path: 'goods/out',
          name: 'GoodsOut',
          component: () => import('@/views/goods/checkList'),
          meta: { title: '拍品出库', icon: 'inout' }
        }
      ]
    },
    {
      path: '/goods',
      component: Layout,
      redirect: 'add',
      hidden: true,
      meta: { roles: ['admin'] },
      children: [
        {
          path: 'add',
          name: 'GoodsAdd',
          component: () => import('@/views/goods/detail'),
          meta: { title: '拍品入库' }
        },
        {
          path: ':id(\\d+)',
          name: 'GoodsEdit',
          component: () => import('@/views/goods/detail'),
          meta: { title: '修改拍品' }
        }
      ]
    },
    {
      path: '/goods',
      component: Layout,
      redirect: '',
      meta: { roles: ['editor'] },
      children: [
        {
          path: '',
          name: 'GoodsManagement',
          component: () => import('@/views/goods/list'),
          meta: { title: '拍品管理', icon: 'inout' }
        },
        {
          path: ':id(\\d+)',
          hidden: true,
          name: 'GoodsEdit',
          component: () => import('@/views/goods/detail'),
          meta: { title: '修改拍品' }
        }
      ]
    },
    {
      path: '/goods/add',
      component: Layout,
      redirect: '',
      meta: { roles: ['editor'] },
      children: [
        {
          path: '',
          name: 'GoodsAdd',
          component: () => import('@/views/goods/detail'),
          meta: { title: '拍品入库', icon: 'form' }
        }
      ]
    },
    {
      path: '/receipt',
      component: Layout,
      redirect: '',
      meta: { roles: ['editor'] },
      children: [
        {
          path: '',
          name: 'InReceipt',
          component: () => import('@/views/goods/checkList'),
          meta: { title: '打印入库单', icon: 'table' }
        }
      ]
    },
    {
      path: '/settle',
      component: Layout,
      meta: { title: '拍品结算', icon: 'pay', roles: ['cashier'] },
      children: [
        {
          path: 'buy',
          name: 'SettleBuy',
          component: () => import('@/views/goods/checkList'),
          meta: { title: '购买人结算', icon: 'user' }
        },
        {
          path: 'send',
          name: 'SettleSend',
          component: () => import('@/views/goods/checkList'),
          meta: { title: '送货人结算', icon: 'user' }
        }
      ]
    },
    {
      path: '/order',
      component: Layout,
      meta: { title: '订单管理', icon: 'form', roles: ['cashier'] },
      children: [
        {
          path: 'buy',
          name: 'OrderBuy',
          component: () => import('@/views/goods/order'),
          meta: { title: '购买人', icon: 'user' }
        },
        {
          path: 'send',
          name: 'OrderSend',
          component: () => import('@/views/goods/order'),
          meta: { title: '送货人', icon: 'user' }
        }
      ]
    },
    {
      path: '/consign',
      component: Layout,
      redirect: '',
      children: [
        {
          path: '',
          name: 'GoodsConsign',
          component: () => import('@/views/goods/consign'),
          meta: { title: '拍品委托', icon: 'consign', roles: ['admin'] }
        }
      ]
    },
    {
      path: '/key',
      component: Layout,
      redirect: '',
      meta: { roles: ['admin', 'cashier'] },
      children: [
        {
          path: '',
          name: 'KeyManagement',
          component: () => import('@/views/sub/key'),
          meta: { title: '密钥管理', icon: 'key' }
        }
      ]
    },
    {
      path: '/staff',
      component: Layout,
      redirect: '',
      meta: { roles: ['admin'] },
      children: [
        {
          path: '',
          name: 'StaffManagement',
          component: () => import('@/views/sub/staff'),
          meta: { title: '员工管理', icon: 'user' }
        }
      ]
    },
    {
      path: '/config',
      component: Layout,
      redirect: '',
      meta: { roles: ['admin'] },
      children: [
        {
          path: '',
          name: 'Config',
          component: () => import('@/views/sub/config'),
          meta: { title: '拍卖配置', icon: 'config' }
        }
      ]
    },
    {
      path: '/password',
      component: Layout,
      redirect: '',
      meta: { roles: ['admin'] },
      children: [{
        path: '',
        component: () => import('@/views/sub/password'),
        name: 'Password',
        meta: { title: '修改密码', icon: 'password' }
      }]
    },
    {
      path: '/logo',
      component: Layout,
      redirect: '',
      meta: { roles: ['admin'] },
      children: [{
        path: '',
        component: () => import('@/views/sub/logo'),
        name: 'Logo',
        meta: { title: 'LOGO', icon: 'dashboard' }
      }]
    },
    // 404 page must be placed at the end !!!
    { path: '*', redirect: '/404', hidden: true }
  ]
}

if (process.env.VUE_APP_ROLE === 'ctrl') {
  asyncRoutes_ctrl = [
    {
      path: '/',
      redirect: '/',
      component: Layout,
      meta: { title: '全部专场', icon: 'lot', roles: ['admin'] },
      children: [
        {
          path: '/',
          name: 'LotList',
          hidden: true,
          component: () => import('@/views/lot/commonList'),
          meta: { breadcrumb: false }
        },
        {
          path: ':id(\\d+)',
          name: 'ControlPanel',
          hidden: true,
          component: () => import('@/views/panel/ctrl'),
          meta: { title: '控制台' }
        }
      ]
    },
    { path: '*', redirect: '/404', hidden: true }
  ]
}

if (process.env.VUE_APP_ROLE === 'aucer') {
  asyncRoutes_aucer = [
    {
      path: '/',
      component: () => import('@/views/screen/aucer'),
      meta: { title: '大屏幕' },
      hidden: true
    },
    { path: '*', redirect: '/404', hidden: true }
  ]
}

if (process.env.VUE_APP_ROLE === 'ctrlbid') {
  asyncRoutes_ctrlbid = [
    {
      path: '/',
      redirect: '/',
      component: Layout,
      meta: { title: '全部专场', icon: 'lot', roles: ['admin'] },
      children: [
        {
          path: '/',
          name: 'LotList',
          hidden: true,
          component: () => import('@/views/lot/commonList'),
          meta: { breadcrumb: false }
        },
        {
          path: ':id(\\d+)',
          name: 'ControlBidPanel',
          hidden: true,
          component: () => import('@/views/panel/ctrlbid'),
          meta: { title: '网络出价控制面板' }
        }
      ]
    },
    { path: '*', redirect: '/404', hidden: true }
  ]
}

if (process.env.VUE_APP_ROLE === 'bidco') {
  asyncRoutes_bidco = [
    {
      path: '/',
      redirect: '/',
      component: Layout,
      meta: { title: '全部专场', icon: 'lot', roles: ['admin'] },
      children: [
        {
          path: '/',
          name: 'LotList',
          hidden: true,
          component: () => import('@/views/lot/commonList'),
          meta: { breadcrumb: false }
        },
        {
          path: ':id(\\d+)',
          name: 'BidCoPanel',
          hidden: true,
          component: () => import('@/views/panel/bidco'),
          meta: { title: '出价面板' }
        }
      ]
    },
    { path: '*', redirect: '/404', hidden: true }
  ]
}

export const asyncRoutes = process.env.VUE_APP_ROLE === 'super' ? asyncRoutes_super
  : process.env.VUE_APP_ROLE === 'sub' ? asyncRoutes_sub
    : process.env.VUE_APP_ROLE === 'ctrl' ? asyncRoutes_ctrl
      : process.env.VUE_APP_ROLE === 'bidco' ? asyncRoutes_bidco
        : process.env.VUE_APP_ROLE === 'aucer' ? asyncRoutes_aucer
          : asyncRoutes_ctrlbid

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter () {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
